package com.sqsf.util;

import com.sqsf.javabean.Account;
import mapper.AccountMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.UUID;

public class AccountTest {
    SqlSession session;
    AccountMapper mapper;
    @Before
    public void a() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
         mapper = session.getMapper(AccountMapper.class);

    }

    @After
    public void b() {
        //5、关闭资源
        session.close();
    }

    @Test
    public void insertAccount(){
        //UUID是不重复的字符串生成样式
        //使用uuid作为主键添加数据
        //1)就是通过代码生成uuid之后添加到数据库
        String uuid = UUID.randomUUID().toString().replace("-", "");
        Account a=new Account(uuid,"a");
        mapper.insertAccount(a);
    }
    @Test
    public void insertAccount1(){

        Account a=new Account(null,"uuid");
        mapper.insertAccount1(a);
    }
}